﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using uTools;
public class Level9Heart : MonoBehaviour
{
    public Color normalColor;
    public uTweenAlpha tweenAlpha;
    public GameLevelItem gameLevelItem;
    public List<Button> btnList = new List<Button>();
    public List<Image> imageList = new List<Image>();
    private List<bool> stateList = new List<bool>();
    private bool result = false;
    void Start()
    {
        InitBtnEvent();
    }

    private void InitBtnEvent()
    {
        int count = btnList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            btnList[tempIndex].onClick.AddListener(() =>
            {
                SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
                BtnClick(tempIndex);
            });
            stateList.Add(false);
        }
    }

    private void BtnClick(int index)
    {
        if (result)
            return;
        stateList[index] = !stateList[index];
        int tempIndex = 0;
        // 左
        tempIndex = index - 1;
        if (tempIndex >= 0 && tempIndex / 3 == index / 3)
            stateList[tempIndex] = !stateList[tempIndex];
        // 右
        tempIndex = index + 1;
        if (tempIndex < 9 && tempIndex / 3 == index / 3)
            stateList[tempIndex] = !stateList[tempIndex];
        // 上
        tempIndex = index - 3;
        if (tempIndex >= 0)
            stateList[tempIndex] = !stateList[tempIndex];
        // 下
        tempIndex = index + 3;
        if (tempIndex < 9)
            stateList[tempIndex] = !stateList[tempIndex];
        UpdateImageColor();
    }

    private void UpdateImageColor()
    {
        bool tempResult = true;
        int count = imageList.Count;
        for (int i = 0; i < count; i++)
        {
            imageList[i].enabled = stateList[i];// ? Color.red : normalColor;
            if (!stateList[i])
                tempResult = false;
        }

        if (tempResult)
        {
            result = true;
            tweenAlpha.Play(PlayDirection.Forward);
            tweenAlpha.onFinished.AddListener(() =>
            {
                gameLevelItem.TriggerResult(true);
            });
        }
    }


}
